package com.bsg.upm.check;

import java.text.MessageFormat;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.bsg.upm.constant.DictTypeConstants;
import com.bsg.upm.constant.Messages;
import com.bsg.upm.entity.BackupStorageEntity;
import com.bsg.upm.entity.BackupStrategyEntity;
import com.bsg.upm.entity.ServEntity;

/**
 * 备份策略业务处理检查
 * 
 * @author HCK
 *
 */
@Service
public class BackupStrategyCheck extends BaseCheck {

	/**
	 * 备份策略新增时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkSave(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkSaveNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkSaveLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 备份策略编辑时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkUpdate(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkUpdateNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkUpdateLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 备份策略启用时检查
	 * 
	 * @param backupStrategy
	 *            备份策略对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkEnable(BackupStrategyEntity backupStrategy) {
		CheckResult chkRS = new CheckResult();
		// 存在性检查
		if (backupStrategy == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "备份策略"));
			return chkRS;
		}

		// 备份策略状态检查
		if (backupStrategy.getEnabled()) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg(Messages.ENABLED_ALREADY);
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 备份策略停用时检查
	 * 
	 * @param backupStrategy
	 *            备份策略对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkDisable(BackupStrategyEntity backupStrategy) {
		CheckResult chkRS = new CheckResult();
		// 存在性检查
		if (backupStrategy == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "备份策略"));
			return chkRS;
		}

		// 备份策略状态检查
		if (!backupStrategy.getEnabled()) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(Messages.DISABLED_ALREADY);
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 备份策略删除时检查
	 * 
	 * @param backupStrategy
	 *            备份策略对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkRemove(BackupStrategyEntity backupStrategy) {
		CheckResult chkRS = new CheckResult();
		// 存在性检查
		if (backupStrategy == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "备份策略"));
			return chkRS;
		}

		// 备份策略状态检查
		if (backupStrategy.getEnabled()) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg(MessageFormat.format(Messages.ENABLED_NOT_DELETE, "备份策略"));
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 新增时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 所属服务非空检查
		String servId = (String) paramMap.get("servId");
		if (StringUtils.isBlank(servId)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "所属服务"));
			return chkRS;
		}

		// 备份类型非空检查
		String type = (String) paramMap.get("type");
		if (StringUtils.isBlank(type)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "备份类型"));
			return chkRS;
		}

		// 备份策略非空检查
		String cronExpression = (String) paramMap.get("cronExpression");
		if (StringUtils.isBlank(cronExpression)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "备份策略"));
			return chkRS;
		}

		// 备份文件保留天数非空检查
		Integer retention = (Integer) paramMap.get("retention");
		if (retention == null) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "备份文件保留天数"));
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 新增时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 所属服务存在性检查
		String servId = (String) paramMap.get("servId");
		ServEntity serv = servDao.getBase(servId);
		if (serv == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "所属服务"));
			return chkRS;
		}

		// 备份类型存在性检查
		String type = (String) paramMap.get("type");
		if (dictTypeCache.getDictFromCache(DictTypeConstants.BACKUP_TYPE, type) == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "备份类型"));
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 编辑时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkUpdateNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 备份类型非空检查
		String type = (String) paramMap.get("type");
		if (StringUtils.isBlank(type)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "备份类型"));
			return chkRS;
		}

		// 备份策略非空检查
		String cronExpression = (String) paramMap.get("cronExpression");
		if (StringUtils.isBlank(cronExpression)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "备份策略"));
			return chkRS;
		}

		// 备份文件保留天数非空检查
		Integer retention = (Integer) paramMap.get("retention");
		if (retention == null) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "备份文件保留天数"));
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 编辑时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkUpdateLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 备份策略存在性检查
		String backupStorageId = (String) paramMap.get("backupStorageId");
		BackupStorageEntity backupStorage = backupStorageDao.get(backupStorageId);
		if (backupStorage == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "备份策略"));
			return chkRS;
		}

		// 备份类型存在性检查
		String type = (String) paramMap.get("type");
		if (dictTypeCache.getDictFromCache(DictTypeConstants.BACKUP_TYPE, type) == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "备份类型"));
			return chkRS;
		}
		return chkRS;
	}
}
